﻿using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Zmc.Apparatu;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Zmc.Apparatu
{
    public class DelApparatusCommendHandlers : IRequestHandler<DelApparatusCommends, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;

        public DelApparatusCommendHandlers(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除器械数据--逻辑删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(DelApparatusCommends request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var del = await db.Queryable<Apparatus>().FirstAsync(p => p.ApparatusId == request.ApparatusId);
            del.IsDeletes = true;
            var res = await db.Updateable<Apparatus>(del).ExecuteCommandAsync();
            if (res > 0)
            {
                result.Code = ApiEnums.删除成功;
                result.Msg = "删除成功";
            }
            else
            {
                result.Code = ApiEnums.删除失败;
                result.Msg = "删除失败";
            }
            return result;
        }
    }
}
